﻿using System;
using System.Collections.Generic;
using Convertor.Interface;
using Wpf.CommonTools.SystemExtension;
using System.Windows;

namespace Convertor.Plugin.SendEmail
{
    public class Plugin : PluginBase
    {

        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.SendEmail;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.SendEmail;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Web") + " - " + UILanguage.FindResource("Plugin_SendEmail_Name"); }
        }

        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_SendEmail_Desc"); }
        }

        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_SendEmail_NoEmailToSend"), LogTypes.Alert); }
                return content;
            }

            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            if (SendEmail.SmtpClientList.Count != config.SmtpClientList.Count)
            {
                SendEmail.SmtpClientList = new List<SendEmail>();
                foreach (var a in config.SmtpClientList)
                {
                    try
                    {
                        SendEmail b = new SendEmail(a);
                    }
                    catch (Exception ex)
                    {
                        if (Log != null) { Log(this, ex.Message, LogTypes.Error | LogTypes.HidePluginName); }
                    }
                }
            }

            if (SendEmail.SmtpClientList.Count == 0 || string.IsNullOrEmpty(config.Body))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_OpenConfigForm"), LogTypes.Normal | LogTypes.HidePluginName); }
                if (Loader.ShowDialog() == true)
                {
                    config = ((IFormGetConfigEvent)Loader.Loader).GetConfig() as ConfigContent;
                }
                else
                {
                    if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_CloseConfigForm"), LogTypes.Error | LogTypes.HidePluginName); }
                    content.StopConvert = true;
                    return content;
                }
            }

            int number = SendEmail.SmtpClientList.Count;
            if (number == 0)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_SendEmail_NoSMPTConfig"), LogTypes.Alert | LogTypes.HidePluginName); }
                return content;
            }

            if (string.IsNullOrEmpty(config.Body))
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_SendEmail_NoBody"), LogTypes.Alert | LogTypes.HidePluginName); }
                return content;
            }

            int t = new Random().Next(0, number - 1);

            SendEmail se = SendEmail.SmtpClientList[t];
            string to = content.OutputContent.ToString().Trim();
            try
            {
                se.SendMail(to, config.Subject, config.Body, config.IsHtml, null);
                if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_SendEmail_Success"), to, se.ToString()), LogTypes.Normal | LogTypes.HidePluginName); }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                Exception inner = ex.InnerException;
                while (inner != null)
                {
                    message += inner.Message + ", ";
                    inner = inner.InnerException;
                }
                if (Log != null) { Log(this, message, LogTypes.Alert | LogTypes.HidePluginName); }
            }
            return new ConvertParamters(content.LastInputContent, content.OutputContent);
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
